package apis

import (
	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
)

type SysBaseConfig struct {
	api.Api
}

func (e SysBaseConfig) GetConfig(c *gin.Context) {
	s := service.SysBaseConfig{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	cfgData, err := s.GetConfig()
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	e.OK(cfgData, "success")
}

func (e SysBaseConfig) SetConfig(c *gin.Context) {
	req := dto.SysBaseConfigData{}
	s := service.SysBaseConfig{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	err = s.SetConfig(req)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "设置成功")
}
